<?php
namespace app\atxunda\controller;
use think\Controller;
use think\Db; 
use think\Request;

class Privilege extends Common
{
	/**
	 * [index 权限管理]
	 * @return [type] [description]
	 */
	public function index(){
		$data = \app\atxunda\model\Privilege::getInfo();
		$this->assign('data',$data);
		return $this->fetch('index');
	}


	/**
	 * [add 添加权限]
	 */
	public function add(){
		if(input('post.')){
			if(input('post.')){
				if(input('post.privilege_name') == '' || input('post.controller_name') == '' || input('post.action_name') == ''){
					$this->error('权限名称/控制器名/方法名为必填项');
				}
				if(!preg_match("/^[A-Za-z]\w+$/", input('post.controller_name'))){
					$this->error('控制器名的首字母必须是大写并且只能是字母');
				}
				/*if(!preg_match("/^[a-z][a-z0-9]\w+$/", input('post.action_name'))){
					$this->error('方法名的第一位只能是字母');
				}*/
			}
			$data = input('post.');
			if(Db::table('privilege')->insert($data)){
				$this->success('添加成功','index','',1);
			}else{
				$this->error('添加失败');
			}
		}else{
			$data = \app\atxunda\model\Privilege::getInfo();
			$this->assign('data',$data);
			return $this->fetch('add');
		}
	}

	/**
	 * [edit 编辑权限]
	 * @return [type] [description]
	 */
	public function edit(){
		$request = Request::instance();
		$privilege_id = $request->param('privilege_id');
		if(input('post.')){
			$data = input('post.');
			if(Db::table('privilege')->where('privilege_id',$data['privilege_id'])->update($data)){
				$this->success('编辑成功','index','',1);
			}else{
				$this->error('编辑失败');
			}
		}else{
			$privilege_data = Db::table('privilege')->where('privilege_id',$privilege_id)->find();
			$data = \app\atxunda\model\Privilege::getInfo();
			$this->assign('data',$data);
			$this->assign('privilege_data',$privilege_data);
			return $this->fetch('edit');
		}
	}


	public function delete(){
		$request = Request::instance();
		$privilege_id = $request->param('privilege_id');
		if(Db::table('privilege')->delete($privilege_id)){
			$this->success('删除成功','index','',1);
		}else{
			$this->error('删除失败');
		}
	}
}